package org.ntu.csn.as.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ntu.csn.as.manager.DBManager;
import org.ntu.csn.as.model.Category;
import org.ntu.csn.as.tool.Constant;

/**
 * Servlet implementation class QuestionDesignController
 */
@WebServlet("/QuestionDesignController")
public class QuestionDesignController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public QuestionDesignController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Question Design Controller ......");
		DBManager dbm = new DBManager();
		int tid = Constant.DEFAULT_TOPIC_ID;
		try {
			tid = Integer.parseInt(request.getParameter("selectedtid"));
		} catch (Exception ex) {
			tid = Constant.DEFAULT_TOPIC_ID;
		}
		try {

			Category c = dbm.getCategory(tid);
			request.setAttribute("selectedTopic", c);
			request.getRequestDispatcher("QuestionMan.jsp").forward(request,
					response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
